package com.example.libraryms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.libraryms.model.dto.BookAddDTO;
import com.example.libraryms.model.dto.BookDetailDTO;
import com.example.libraryms.model.dto.BookQueryDTO;
import com.example.libraryms.model.dto.BookUpdateDTO;
import com.example.libraryms.model.entity.Book;
import com.example.libraryms.service.BookService;
import com.example.libraryms.utils.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Cauli
 * @date 2023/7/31 11:52
 * @description 图书 前端控制器
 */
@Api(tags = "图书")
@ApiSort(1)
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取图书列表")
    @GetMapping("/list")
    public ResultResponse<Book> listBook(BookQueryDTO bookQueryDTO) {
        Page<Book> bookPage = bookService.getBookPage(bookQueryDTO);
        return ResultResponse.success().dataList(bookPage.getRecords(), bookPage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "根据图书编号获取详细信息")
    @GetMapping(value = { "/{bookNo}"})
    public ResultResponse<BookDetailDTO> getBookDetail(@PathVariable(value = "bookNo", required = false) Integer bookNo) {
        BookDetailDTO bookDetailDTO = bookService.getBookDetail(bookNo);
        return ResultResponse.success().data(bookDetailDTO);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "添加图书")
    @PostMapping
    public ResultResponse<Void> addBook(@RequestBody BookAddDTO bookAddDTO) {
        bookService.addBook(bookAddDTO);
        return ResultResponse.success().message("添加成功");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改图书")
    @PutMapping
    public ResultResponse<Void> updateBook(@RequestBody BookUpdateDTO bookUpdateDTO) {
        bookService.updateBook(bookUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除图书")
    @DeleteMapping("/{bookNoList}")
    public ResultResponse<Void> deleteBook(@PathVariable List<Integer> bookNoList) {
        bookService.deleteBooks(bookNoList);
        return ResultResponse.success().message("删除成功");
    }
}
